package orange.com.openstack.sahara.api;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import net.minidev.json.JSONObject;
import orange.com.openstack.sahara.data.Image;
import orange.com.openstack.sahara.httpConnection.RestCall;

import com.google.gson.Gson;
import com.jayway.jsonpath.JsonPath;

/**
 * class allowing the interaction with the cluster Rest service
 * 
 * @author osvaldo.cocucci@gmail.com
 * 
 */
public class Images {

	String endpoint;
	String tenantId;
	String token;
	Gson gson;

	/**
	 * @param endpoint
	 * @param tenantId
	 * @param token
	 * 
	 *            constructor
	 */
	public Images(String endpoint, String tenantId, String token) {
		gson = new Gson();
		this.endpoint = endpoint;
		this.tenantId = tenantId;
		this.token = token;
	}

	/**
	 * 
	 * This operation returns the list of all Clusters.
	 */
	public List<Image> listImage() {
		List<Image> dataSources = new ArrayList<Image>();
		RestCall caller = new RestCall();
		HttpURLConnection connection = caller.connect(endpoint + tenantId
				+ "/images", null, "GET", token);
		try {
			if (connection.getResponseCode() != 200)
				return null;
			String JSONreponse = caller.getResponse(connection);
			connection.disconnect();
			List<JSONObject> dataSourcesJSON = JsonPath.read(JSONreponse,
					"$.images[*]");

			for (JSONObject json : dataSourcesJSON) {
				dataSources
						.add(gson.fromJson(json.toJSONString(), Image.class));
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return dataSources;
	}

	/**
	 * @param dataSourceId
	 * 
	 *            This operation shows information about a specified Cluster.
	 */
	public Image showImage(String imageId) {
		RestCall caller = new RestCall();
		HttpURLConnection connection = caller.connect(endpoint + tenantId
				+ "/images/" + imageId, null, "GET", token);
		try {
			if (connection.getResponseCode() != 200)
				return null;
			String JSONreponse = caller.getResponse(connection);
			connection.disconnect();

			return gson.fromJson(((JSONObject) JsonPath.read(JSONreponse,
					"$.cluster")).toJSONString(), Image.class);

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param username
	 * @param description
	 * @param imageId
	 * 
	 * @return the registered Image information
	 */
	public Image registerImage(String username, String description,
			String imageId) {
		RestCall caller = new RestCall();
		String payload = "{    \"username\": \"" + username
				+ "\",    \"description\": \"" + description + "}";

		HttpURLConnection connection = caller.connect(endpoint + tenantId
				+ "/images/" + imageId, payload, "POST", token);
		try {
			if (connection.getResponseCode() != 202)
				return null;
			String JSONreponse = caller.getResponse(connection);
			connection.disconnect();
			return gson.fromJson(((JSONObject) JsonPath.read(JSONreponse,
					"$.image")).toJSONString(), Image.class);

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param image_id
	 * 
	 *            Remove an Image from the Image Registry
	 */
	public boolean deleteImage(String image_id) {
		RestCall caller = new RestCall();
		HttpURLConnection connection = caller.connect(endpoint + tenantId
				+ "/images/" + image_id, null, "DELETE", token);
		try {
			if (connection.getResponseCode() == 204)
				return true;
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
}
